﻿namespace Pub.Class
{
    using System;
    using System.Runtime.CompilerServices;

    public static class LongExtensions
    {
        public static string FormatBytes(this long bytes)
        {
            if (bytes <= 999.0)
            {
                return (bytes.ToString() + " bytes");
            }
            if (bytes <= 1022976.0)
            {
                return ((((double) bytes) / 1024.0).ThreeNonZeroDigits() + " KB");
            }
            if (bytes <= 1047527424.0)
            {
                return ((((double) bytes) / 1048576.0).ThreeNonZeroDigits() + " MB");
            }
            if (bytes <= 1072668082176)
            {
                return ((((double) bytes) / 1073741824.0).ThreeNonZeroDigits() + " GB");
            }
            if (bytes <= 1098412116148224)
            {
                return ((((double) bytes) / 1099511627776).ThreeNonZeroDigits() + " TB");
            }
            if (bytes <= 1.1247740069357814E+18)
            {
                return ((((double) bytes) / 1125899906842624).ThreeNonZeroDigits() + " PB");
            }
            if (bytes <= 1.1517685831022401E+21)
            {
                return ((((double) bytes) / 1.152921504606847E+18).ThreeNonZeroDigits() + " EB");
            }
            if (bytes <= 1.1794110290966939E+24)
            {
                return ((((double) bytes) / 1.1805916207174113E+21).ThreeNonZeroDigits() + " ZB");
            }
            return ((((double) bytes) / 1.2089258196146292E+24).ThreeNonZeroDigits() + " YB");
        }

        public static string FormatKB(this long bytes)
        {
            if (bytes <= 999.0)
            {
                return (bytes.ToString() + " bytes");
            }
            return ((((double) bytes) / 1024.0).ThreeNonZeroDigits() + " KB");
        }

        public static long IfEqual(this long obj, long value, long defaultValue)
        {
            if (obj != value)
            {
                return obj;
            }
            return defaultValue;
        }

        public static long IfLessThan(this long obj, long value, long defaultValue)
        {
            if (obj >= value)
            {
                return obj;
            }
            return defaultValue;
        }

        public static long IfLessThanOrEqual(this long obj, long value, long defaultValue)
        {
            if (obj > value)
            {
                return obj;
            }
            return defaultValue;
        }

        public static long IfMoreThan(this long obj, long value, long defaultValue)
        {
            if (obj <= value)
            {
                return obj;
            }
            return defaultValue;
        }

        public static long IfMoreThanOrEqual(this long obj, long value, long defaultValue)
        {
            if (obj < value)
            {
                return obj;
            }
            return defaultValue;
        }

        public static long IfNotEqual(this long obj, long value, long defaultValue)
        {
            if (obj == value)
            {
                return obj;
            }
            return defaultValue;
        }

        public static decimal PercentageOf(this long number, decimal percent)
        {
            return ((number * percent) / 100M);
        }

        public static decimal PercentageOf(this long number, double percent)
        {
            return (decimal) ((number * percent) / 100.0);
        }

        public static decimal PercentageOf(this long number, int percent)
        {
            return ((number * percent) / 100L);
        }

        public static decimal PercentageOf(this long number, long percent)
        {
            return ((number * percent) / 100L);
        }

        public static decimal PercentageOf(this long number, float percent)
        {
            return (decimal) ((number * percent) / 100f);
        }

        public static decimal PercentOf(this long position, decimal total)
        {
            decimal num = 0M;
            if ((position > 0L) && (total > 0M))
            {
                num = (position / total) * 100M;
            }
            return num;
        }

        public static decimal PercentOf(this long position, double total)
        {
            decimal num = 0M;
            if ((position > 0L) && (total > 0.0))
            {
                num = (position / ((decimal) total)) * 100M;
            }
            return num;
        }

        public static decimal PercentOf(this long position, int total)
        {
            decimal num = 0M;
            if ((position > 0L) && (total > 0))
            {
                num = (position / total) * 100M;
            }
            return num;
        }

        public static decimal PercentOf(this long position, long total)
        {
            decimal num = 0M;
            if ((position > 0L) && (total > 0L))
            {
                num = (position / total) * 100M;
            }
            return num;
        }

        public static decimal PercentOf(this long position, float total)
        {
            decimal num = 0M;
            if ((position > 0L) && (total > 0f))
            {
                num = (position / ((decimal) total)) * 100M;
            }
            return num;
        }

        public static string ThreeNonZeroDigits(this double value)
        {
            if (value >= 100.0)
            {
                int num = (int) value;
                return num.ToString();
            }
            if (value >= 10.0)
            {
                return value.ToString("0.0");
            }
            return value.ToString("0.00");
        }

        public static string ToCurrency(this long value)
        {
            return value.ToString("N");
        }

        public static string ToRMB(this long value)
        {
            return value.ToRMB();
        }

        public static string ToTime(this long seconds)
        {
            long num = seconds / 0xe10L;
            long num2 = (seconds - (num * 0xe10L)) / 60L;
            seconds = seconds % 60L;
            return (((num < 10L) ? ("0" + num.ToString()) : num.ToString()) + ":" + ((num2 < 10L) ? ("0" + num2.ToString()) : num2.ToString()) + ":" + ((seconds < 10L) ? ("0" + seconds.ToString()) : seconds.ToString()));
        }
    }
}

